//
//  VisitorLoginView.swift
//  Weibo
//
//  Created by apple on 15/11/26.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

//定义访客视图的协议   optional 是OC中关键字
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //协议提供方法
    
    //登录的协议方法 可选协议方法
    optional func userWilllogin()
    //必选的协议方法
    //注册的协议方法
    func userWillRegister()
}

class VisitorLoginView: UIView {
    
    //提供代理对象 weak
    //在swift中 所有的属性 默认都是strong
    weak var visitorViewDelegate: VisitorLoginViewDelegate?
    
    /**
     对外提供设置访客视图的信息
     
     - parameter tipText:   提示文案
     - parameter imageName: 提示的对应的图片
     */
    func setWithInfo(tipText: String,imageName: String?) {
        tipLabel.text = tipText
        if let name = imageName {
            circleView.image = UIImage(named: name)
            iconView.hidden = true
            //将圆圈视图 放到最顶层
            bringSubviewToFront(circleView)
        } else {
            //首页的圆圈视图 添加动画效果
            startAnimation()
        }
    }
    
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        anim.toValue = 2 * M_PI
        //完成动画 & 当视图处于非活跃状态  将动画移除图层
        anim.removedOnCompletion = false
        
        //添加动画
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //添加子视图
        
        
        //设置布局
        //VFL 进行自动布局
        /**
        *  将约束添加到父视图
        *
        *  @param item          添加约束的对象
        *  @param attribute     要添加约束的属性
        *  @param relatedBy     相对的约束  一般使用 精准约束  equal
        *  @param toItem        添加的约束 相对于的视图
        *  @param attribute     相对约束的对象的属性
        *  @param multiplier    约束的关系乘积值
        *  @param constant      约束值
        *
        *  @return "view1.attr1 = view2.attr2 * multiplier + constant
        */
        
        //使用VFL手动添加约束 必须将frame 设置视图的位置的属性 关闭
        
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        
        //按钮的约束
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        //添加遮罩视图的约束
        //OC中有多个枚举选项 使用 按位或  swift-> 给定数组
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskView]-0-|", options:[] , metrics: nil, views: ["maskView": backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-(-35)-[regBtn]", options:[] , metrics: nil, views: ["maskView": backView,"regBtn": registerBtn]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        
        //添加点击事件
        loginBtn.addTarget(self, action: "loginDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerDidClick", forControlEvents: .TouchUpInside)
    }
    
    
    //响应方法
    @objc private func loginDidClick() {
        //visitorViewDelegate?判断代理是否为空
        //userWilllogin! 必须响应登录方法
        //userWilllogin? 是否响应协方法  responderSelector
        visitorViewDelegate?.userWilllogin?()
    }
    
    @objc private func registerDidClick() {
        visitorViewDelegate?.userWillRegister()
    }
    
    
    //懒加载子视图控件
    //圆圈
    
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示文案
    private lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.text = "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜"
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        //设置行数
        l.numberOfLines = 0
        //设置居中显示
        l.textAlignment = NSTextAlignment.Center
        //设置大小
        l.sizeToFit()
        return l
    }()
    
    //登录按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("登录", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.sizeToFit()
        return btn
        
    }()
    
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.sizeToFit()
        return btn
    }()
    
    
}
